#pragma once
#include <jsoncpp/json/json.h>
#include <string>
#include <uuid/uuid.h>
// -------- JSON 序列化 和 反序列化 -----------
class JsopRpc
{
public:
    static bool Serialize(const Json::Value &root, std::string &outStr)
    {
        // 生命周期随线程，避免重复创建
        thread_local static Json::StreamWriterBuilder factor;
        thread_local static Json::StreamWriter *writer = factor.newStreamWriter();

        // using OStream = std::ostream;
        std::stringstream ss;
        writer->write(root, &ss);

        if (!ss.good())
        {
            return false;
        }

        outStr = ss.str();
        return true;
    }

    static bool UnSerialize(const std::string &inStr, Json::Value &outRoot, std::string &err)
    {
        thread_local static Json::CharReaderBuilder factor;
        thread_local static Json::CharReader *reader = factor.newCharReader();

        // 区间：左闭右开 [)
        return reader->parse(inStr.c_str(), inStr.c_str() + inStr.size(), &outRoot, &err);
    }
};

// --------------- uuid生成 --------------
// 生成版本4（随机）UUID
class UUID
{
public:
    static std::string generate_uuid_v4()
    {
        // typedef unsigned char uuid_t[16];
        std::array<unsigned char, 16> out;

        uuid_generate_random(out.data());

        std::array<char, 37> tmp = {0};
        uuid_unparse(out.data(), tmp.data());
        return std::string(tmp.data(), 36);
    }
};